package com.starsoft.smdc.crawler.xueqiu;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.starsoft.smdc.crawler.common.CrawlerService;

public class XueqiuService extends CrawlerService {

	private static Logger logger = LoggerFactory.getLogger(XueqiuService.class);

	private final String XUEQIU = "xueqiu";
	protected static long lastRefresh;
	private String xueqiuParent;
	
	protected String convertSecId(String secId, boolean isToLocal) {
		if (isToLocal) {
			String ticker = secId.substring(secId.length()-6);
			String exchange = secId.substring(0, secId.length()-6);
			exchange = flexsetService.codeConvertFrom(SMDC_EXCHANGE, exchange, XUEQIU);
			return ticker + "." + exchange;
		} else {
			int index = secId.indexOf(".");
			String ticker = secId.substring(0, index);
			String exchange = secId.substring(index + 1);
			exchange = flexsetService.codeConvertTo(SMDC_EXCHANGE, exchange, XUEQIU);
			return exchange + ticker;
		}
	}

	@Override
	protected String getTargetSystem() {
		return XUEQIU;
	}

	protected synchronized HttpClient getHttpClient() {
		try {
			if (httpClient == null) {
				logger.info("Xueqiu get HttpClient: " + xueqiuParent);
				httpClient = HttpClients.createDefault();
				HttpGet post = new HttpGet(xueqiuParent);
				post.setHeaders(defaultHeaders(xueqiuParent));
				HttpResponse httpresponse = httpClient.execute(post);
				EntityUtils.toString(httpresponse.getEntity());
				lastRefresh = System.currentTimeMillis();
			} else if (System.currentTimeMillis() - lastRefresh > 30 * 60 * 1000) {
				logger.info("Xueqiu refesh HttpClient: " + xueqiuParent);
				httpClient = HttpClients.createDefault();
				HttpGet post = new HttpGet(xueqiuParent);
				post.setHeaders(defaultHeaders(xueqiuParent));
				HttpResponse httpresponse = httpClient.execute(post);
				EntityUtils.toString(httpresponse.getEntity());
				lastRefresh = System.currentTimeMillis();
			}
			return httpClient;
		} catch (Exception e) {
			throw new RuntimeException("Xueqiu get HttpClient Failed", e);
		}
	}

	protected Header[] defaultHeaders(String url) {
		List<Header> headers = new ArrayList<>();
		headers.add(new BasicHeader("User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"));
		
		if(url.contains("stock.xueqiu")) {
			headers.add(new BasicHeader("Host", "stock.xueqiu.com"));
		} else {
			headers.add(new BasicHeader("Host", "xueqiu.com"));
			headers.add(new BasicHeader("Referer", "http://xueqiu.com/P/ZH003694"));
		}
		headers.add(new BasicHeader("Pragma", "no-cache"));
		headers.add(new BasicHeader("Connection", "keep-alive"));
		headers.add(new BasicHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"));
		headers.add(new BasicHeader("Accept-Encoding", "gzip, deflate, br"));
		headers.add(new BasicHeader("Cache-Conrol", "no-cache"));
		headers.add(new BasicHeader("X-Requested-With", "XMLHttpRequest"));
		headers.add(new BasicHeader("Accept-Language", "zh-CN,zh;q=0.8"));
		return headers.toArray(new Header[0]);
	}

	public String getXueqiuParent() {
		return xueqiuParent;
	}

	public void setXueqiuParent(String xueqiuParent) {
		this.xueqiuParent = xueqiuParent;
	}

}
